package com.example.leepong.Activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.example.leepong.DataHolder;
import com.example.leepong.Game.Events.GameStateListener;
import com.example.leepong.GameSurface;
import com.example.leepong.R;

public class PlayActivity extends Activity {

    private static final int PAUSE_REQUEST = 1;
    GameSurface _gameSurface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // global errors handler
        //  Thread.SetDefaultUncaughtExceptionHandler(new ExceptionHandler());

        // set content
        //   _gameSurface = new GameSurface(this, null);

        setContentView(R.layout.play);

        _gameSurface = (GameSurface) findViewById(R.id.gameSurface);
        _gameSurface.Initilize(DataHolder.StageConfiguration);

        _gameSurface.Engine.EventsBroadcaster.ListenToGameState(new GameStateListener() {
            @Override
            public void OnGamePaused() {
                PauseGame();
            }

            @Override
            public void OnGameResumed() {
            }
        });
    }

    public void PauseGame() {
        _gameSurface.Engine.Pause(false);
        Intent playScreen = new Intent(getApplicationContext(), PauseActivity.class);
        startActivityForResult(playScreen, PAUSE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == PAUSE_REQUEST) {
            _gameSurface.Engine.Resume(false);
        }
    }

    @Override
    public void onBackPressed() {
        PauseGame();
    }

    @Override
    protected void onRestart() {
        if (_gameSurface.Engine.IsRunning)
            PauseGame();
        super.onRestart();
    }
}
